package astar;

import java.util.List;

import interfaces.Crossing;

public class AStarNodeImpl implements AStarNode{

	private Crossing crossing;
	private AStarNode successorNode;
	
	private double h;
	private double g;
	private double f;
	
	private List<AStarNode> neighbors;
	
	/**Konstruktor
	 * @param crossing Kreuzung des Nodes
	 * @param goalNode Zielknoten des AStern
	 */
	public AStarNodeImpl(Crossing crossing, AStarNode goalNode) {
		this.crossing = crossing;
		this.successorNode = null;
		if(goalNode == null){
			setH(0);
		}else{
			setH(calculateH(goalNode));
		}
		this.neighbors = null;
	}

	/**Berechnet das H des Knoten
	 * @param goalNode Zielknoten des AStern
	 * @return H des Knoten
	 */
	private double calculateH(AStarNode goalNode){
		return crossing.getCoordinate().distanceTo(goalNode.getCrossing().getCoordinate());
	}
	
	/**Berechnet das F des Knoten
	 */
	private void calculateF(){
		this.f = this.h + this.g;
	}

	@Override
	public Crossing getCrossing() {
		return crossing;
	}

	@Override
	public AStarNode getSuccessorNode() {
		return successorNode;
	}

	@Override
	public void setSuccessor(AStarNode node) {
		this.successorNode = node;
	}
	
	@Override
	public double getH(){
		return h;
	}
		
	/**Setzt das H des Knotens
	 * @param h H des Knotens
	 */
	private void setH(double h){
		this.h = h;
		calculateF();
	}
	
	@Override
	public double getG(){
		return g;
	}

	@Override
	public void setG(double g){
		this.g = g;
		calculateF();
	}
	
	@Override
	public double getF(){
		return f;
	}
	
	@Override
	public boolean equals(AStarNode node){
		if(this.crossing == node.getCrossing()){
			return true;
		}
		return false;
	}

	@Override
	public void setNeighbors(List<AStarNode> neighbors) {
		this.neighbors = neighbors;
	}

	@Override
	public List<AStarNode> getNeighbors() {
		return neighbors;
	}
}
